//
// Created by enemy1205 on 2021/8/12.
//
#include "identity.h"

using namespace std;

void student_re::ID_init(string &name, string &passport, int &num) {
    cout << "您现在以学生的身份操作" << endl;
    get_name() = name;
    get_passport() = passport;
    this->stu_num = num;
}

void student_re::applyOrder(Manager *manager) {

    cout << "机房开放时间为周一至周五！" << endl;
    cout << "请输入申请预约的时间：" << endl;
    cout << "1、周一" << endl;
    cout << "2、周二" << endl;
    cout << "3、周三" << endl;
    cout << "4、周四" << endl;
    cout << "5、周五" << endl;
    int date = 0;
    int interval = 0;
    int room = 0;
    while (true) {
        cin >> date;
        if (date >= 1 && date <= 5) {
            break;
        }
        cout << "输入有误，请重新输入" << endl;
    }
    cout << "请输入申请预约的时间段：" << endl;
    cout << "1、上午" << endl;
    cout << "2、下午" << endl;

    while (true) {
        cin >> interval;
        if (interval == 1 || interval == 2) {
            break;
        }
        cout << "输入有误，请重新输入" << endl;
    }
    cout << "请选择机房：" << endl;
    cout << "1号机房容量：" << manager->get_Com()[0].get_num() << endl;
    cout << "2号机房容量：" << manager->get_Com()[1].get_num() << endl;
    cout << "3号机房容量：" << manager->get_Com()[2].get_num() << endl;
    while (true) {
        cin >> room;
        if (room >= 1 && room <= 3) {
            break;
        }
        cout << "输入有误，请重新输入" << endl;
    }
    cout << "预约成功！审核中" << endl;
    ofstream ofs(ORDER_FILE, ios::app);
    ofs << "date:" << date << " ";
    ofs << "interval:" << interval << " ";
    ofs << "stuId:" << this->get_num() << " ";
    ofs << "stuName:" << this->get_name() << " ";
    ofs << "roomId:" << room << " ";
    ofs << "status:" << 1 << endl;
    ofs.close();
    getchar();
}

bool student_re::showMyOrder() {
    bool has_order= false;
    OrderFile OF;
    /*每行预约迭代器*/
    for (auto it = OF.m_orderData.begin(); it != OF.m_orderData.end(); it++) {
        /*预约中各信息迭代器*/
        /*核对name*/
//        cout << it->first << endl;
//        for ( auto it_m = it->second.begin();it_m!=it->second.end(); it_m++) {
//            cout << it_m->first<< endl;
//        }
        auto it_m = next(it->second.begin(), 5);/*map内部顺序并非按照make_pair时顺序，会默认排序，故应先手动输出一下*/
        /*实际顺序  date  interval  roomId  status  stuId  stuName*/
//        advance(it_m,5);/*advance是引用方式，实际改变迭代器位置，next和prev均返回一个新的迭代器，不改变传入迭代器位置*/
        if (it_m->second == this->get_name()) {
            cout << "预约日期： 周" << prev(it_m, 5)->second;

            cout << " 时段：" << ((prev(it_m, 4))->second == "1" ? "上午" : "下午");
            cout << " 机房：" << (prev(it_m, 3)->second);
            string status = " 状态： ";  // 0 取消的预约   1 审核中   2 已预约 -1 预约失败
            advance(it_m, -2);
            if (it_m->second == "1") {
                status += "审核中";
            } else if (it_m->second == "2") {
                status += "预约成功";
            } else if (it_m->second == "-1") {
                status += "审核未通过，预约失败";
            } else {
                status += "预约已取消";
            }
            cout << status << endl;
            has_order= true;
        }
    }
    return has_order;
}

void student_re::showAllOrder() {
    OrderFile OF;
    if (OF.m_Size == 0) {
        cout << "无预约记录" << endl;
        getchar();
        return;
    }

    for (int i = 0; i < OF.m_Size; i++) {
        cout << i + 1 << "、 ";
        /*利用关键字进行索引*/
        cout << "预约日期： 周" << OF.m_orderData[i]["date"];
        cout << " 时段：" << (OF.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
        cout << " 学号：" << OF.m_orderData[i]["stuId"];
        cout << " 姓名：" << OF.m_orderData[i]["stuName"];
        cout << " 机房：" << OF.m_orderData[i]["roomId"];
        string status = " 状态： ";  // 0 取消的预约   1 审核中   2 已预约 -1 预约失败
        if (OF.m_orderData[i]["status"] == "1") {
            status += "审核中";
        } else if (OF.m_orderData[i]["status"] == "2") {
            status += "预约成功";
        } else if (OF.m_orderData[i]["status"] == "-1") {
            status += "审核未通过，预约失败";
        } else {
            status += "预约已取消";
        }
        cout << status << endl;
    }

    getchar();
}

void student_re::cancelOrder() {
    if (!this->showMyOrder()) {
        cout << "未检测到您当前的预约，请先查询后再试" << endl;
        return;
    }
    cout << "审核中或预约成功的记录可以取消，1代表取消,0代表返回" << endl;
    OrderFile OF;
    int select = 0;
    cin >> select;
    while (true) {
        if (select==1) {
            for (auto it=OF.m_orderData.begin();it!=OF.m_orderData.end();it++) {
                if (it->second["stuName"]==this->get_name()) {
                    it->second["status"] = "0";
                    OF.updateOrder();
                    cout << "已取消预约" << endl;
                    return;
                }
            }
        }
        if (select == 0)return;
        else {
            cout << "输入有误，请重新输入" << endl;
            getchar();
        }
    }
}

void student_re::operMenu(const string &name, Manager *manager) {
    cout << "欢迎学生代表：" << name << "登录！" << endl;
    while (true) {
        cout << "\t\t ----------------------------------\n";
        cout << "\t\t|                                 |\n";
        cout << "\t\t|          1.申请预约              |\n";
        cout << "\t\t|                                 |\n";
        cout << "\t\t|          2.查看我的预约           |\n";
        cout << "\t\t|                                 |\n";
        cout << "\t\t|          3.查看所有预约           |\n";
        cout << "\t\t|                                 |\n";
        cout << "\t\t|          4.取消预约              |\n";
        cout << "\t\t|                                 |\n";
        cout << "\t\t|          0.注销登录              |\n";
        cout << "\t\t|                                 |\n";
        cout << "\t\t ----------------------------------\n";
        cout << "请选择您的操作： " << endl;
        int select = 0;
        cin >> select;
        switch (select) {
            case 1: { //申请预约
                this->applyOrder(manager);
                getchar();
                break;
            }
            case 2: {//查看自身预约
                if (!this->showMyOrder()) cout << "无预约记录" << endl;
                getchar();
                break;
            }
            case 3: {//查看所有预约
                this->showAllOrder();
                getchar();
                break;
            }
            case 4: {//取消预约
                this->cancelOrder();
                getchar();
                break;
            }
            default: {
                cout << "注销成功" << endl;
                return;
            }
        }
    }
}